package com.ktjiaoyu.crm.controller;

import com.ktjiaoyu.crm.base.shiro.MyShiroRealm;
import com.ktjiaoyu.crm.pojo.Role;
import com.ktjiaoyu.crm.pojo.User;
import com.ktjiaoyu.crm.service.RoleService;
import com.ktjiaoyu.crm.service.UserService;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;

import org.apache.shiro.mgt.RealmSecurityManager;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;


    //登出
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        SecurityUtils.getSubject().logout();//shiro注销
        return "redirect:/login";//使用重定向，删除Cookie
    }
    //列表
    @RequestMapping(value = "/user/list")
    public String list(Model model, String usrName, @RequestParam(required = false,defaultValue = "0")Long roleId,
                       @RequestParam(required = false,defaultValue = "1")int pageIndex){

        Sort sort=Sort.by(Sort.Direction.ASC,"usrId");
        //控制分页的辅助类，可以设置页码（从0开始！！！）、每页的数据条数、排序等
        Pageable pageable= PageRequest.of(pageIndex-1,5,sort);
        Page<User> userPager=userService.findUsers(usrName,roleId,pageable);
        model.addAttribute("userPager",userPager);
        //数据回显
        model.addAttribute("usrName",usrName);
        model.addAttribute("roleId",roleId);
        //查询所有角色
        List<Role> roles=roleService.findAllRoles();
        model.addAttribute("roles",roles);
        return "user/list";
    }
    //跳转到新增页面
    @RequestMapping("/user/add")
    public String add(Model model){
        List<Role> roles=roleService.findAllRoles();
        model.addAttribute("roles",roles);
        return "user/add";
    }
    //新增或者修改
    @RequestMapping("/user/save")
    public String save(User user){
        userService.saveUser(user);
        //清空权限缓存
        RealmSecurityManager rsm= (RealmSecurityManager) SecurityUtils.getSecurityManager();
        MyShiroRealm realm=(MyShiroRealm) rsm.getRealms().iterator().next();
        realm.clearMyCachedAuthorizationInfo();
        return "redirect:/user/list";
    }
    //跳转到修改页面
    @RequestMapping("/user/edit")
//    @RequiresPermissions(value = "角色修改 ")
    public String edit(Long usrId, Model model){
        User user=userService.getUser(usrId);
        List<Role> roles=roleService.findAllRoles();
        model.addAttribute("user",user);
        model.addAttribute("roles",roles);
        return "user/edit";
    }
    //删除
    @RequestMapping("/user/del")
    @ResponseBody
    public Map del(Long usrId){
        userService.deleteUser(usrId);
        Map map=new HashMap();
        map.put("delResult","true");
        return map;
    }

}
